﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using System.IO;

using Might.Infrastructure.DOM;
using Might.Infrastructure;

namespace Might.UIControls.FileSystemBrowser {
    public partial class FileSystemBrowserView : IFileSystemBrowserView {
        
        private readonly Dictionary<IDomNode, TreeViewItem> items = new Dictionary<IDomNode, TreeViewItem>();        
        private MouseDoubleClickManager doubleClickManager;
        
        public event DoubleClickEventHandler FSTreeDoubleClick;
        
        public FileSystemBrowserView() {
            InitializeComponent();

            doubleClickManager = new MouseDoubleClickManager( FSTree );
            doubleClickManager.DoubleClick += doubleClickManager_DoubleClick;
        }

        bool IFileSystemBrowserView.IsEnabled {
            get { return this.IsEnabled; }
            set {                                                           
                //TODO : jak to ze se to neaplikuje na vsechny podelementy? Pozor enabled nesmi byt false, jinak to nejde...
                /* if( value ) {
                    this.Cursor = Cursors.Arrow;
                    FSTree.Cursor = Cursors.Arrow;
                } else {
                    this.Cursor = Cursors.Wait;
                    FSTree.Cursor = Cursors.Wait;
                } */
                this.IsEnabled = value;               
            }
        }

        public void AddRoot(IUserInfo userInfo) {
            
            Clear();

            TreeViewItem item;
            //in tree are no items
            if (FSTree.Items.Count == 0) {
                item = new TreeViewItem();
                FSTree.Items.Add(item);
                items.Add(userInfo, item);
            } else {
                //tree has already added root
                item = FSTree.Items[0] as TreeViewItem;
            }
            item.Tag = userInfo;
            item.Header = userInfo.Name;
            item.IsExpanded = true;
  
        }

        public void AddItem(IDomNode toItem, IDomNode item) {
            if (items.ContainsKey(toItem)) {
                //create new tree item add fill with data        
                var treeItem = new TreeViewItem {Tag = item};
                treeItem.MouseLeftButtonUp += doubleClickManager.HandleClick;
                if (item is IFileSystemRoot) {
                    treeItem.Header = (item as IFileSystemRoot).Name;
                } else if (item is IFile) {
                    treeItem.Header = (item as IFile).Name;
                }

                //add tree view item to the tree
                items[toItem].Items.Add(treeItem);
                //update collection                                                
                items.Add(item, treeItem);
            } else {
                throw new ArgumentException("Treeview does not contain this IDomItem");
            }
        }

        public void Clear() {
            FSTree.Items.Clear();
            items.Clear();
        }
        
        protected void onFSTreeDoubleClick( object sender , IDomNode data ) {            
            if( FSTreeDoubleClick != null ) {                                
                FSTreeDoubleClick( sender , new DoubleClickEventArgs() { DomItem = data } );
            }            
        }

        private void doubleClickManager_DoubleClick( object sender , MouseButtonEventArgs e ) {
            if( FSTree.SelectedItem != null ) {
                onFSTreeDoubleClick( sender , ( FSTree.SelectedItem as TreeViewItem ).Tag as IDomNode );
                e.Handled = true;
            }
        }                         

    }
}
